const base = require("./base");
const moment = require("moment");

module.exports = class extends base {
    async _initialize() {
        await super.isWxaAuth();
    }

    async check() {
        const appId = this.state.wxa.app_id;
        const app = await this.model("app")
            .query(qb => {
                qb.where("id", appId);
                qb.where(
                    "started_at",
                    "<=",
                    moment().format("YYYY-MM-DD HH:mm:ss")
                );
                qb.where(
                    "ended_at",
                    ">=",
                    moment().format("YYYY-MM-DD HH:mm:ss")
                );
            })
            .fetch({ withRelated: [] });
        if (!app) {
            this.fail("小程序服务已到期", "Wxa Time Unauthorized");
            return false;
        }
        this.success();
    }
};
